package com.waterleak;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;

import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.android.*;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.*;
import com.facebook.model.GraphUser;
import com.waterleak.Utility;


public class LoginFB extends Activity {

	private Facebook facebook;
	private AsyncFacebookRunner asynRunner;
	private Handler handler;
	private TextView text;
	private ImageView imgUserPic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_fb);
		
		handler =new Handler();
		text=(TextView)findViewById(R.id.text);
	    imgUserPic=(ImageView)findViewById(R.id.user_pic);
		
		facebook=new Facebook("504165516345357");
		
		asynRunner =new AsyncFacebookRunner(facebook);
		facebook.authorize(this, new DialogListener(){
	     @Override
	     public void onComplete(Bundle values){
	     
	     text.setText("กำลังดึงชื่อและภาพโปรไฟล์.....");
	     Bundle params=new Bundle();
	     params.putString("fields", "name, picture");
	     asynRunner.request("me", params,new UserRequestListener());
	     }

		@Override
		public void onFacebookError(FacebookError e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onError(DialogError e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			
		}
	     
		});
	     
		}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login_fb, menu);
		return true;
	}
	
	@Override
	public void onActivityResult(int requestCode,int resultCode,Intent data)
	{
		 super.onActivityResult(requestCode, resultCode, data);
		 facebook.authorizeCallback(requestCode, resultCode, data);
	}



public  class UserRequestListener implements RequestListener{

	@Override
	public void onComplete(final String response,final Object state) {
		// TODO Auto-generated method stub
		 JSONObject jsonObject;
		 //JSONObject urltest;
	
		try{
			jsonObject=new JSONObject(response);
			Log.d("response write ", "text = "+response);
			final String name=jsonObject.getString("name");
			//inal String picURL=jsonObject.getString("picture");
			JSONObject obj = jsonObject.getJSONObject("picture").getJSONObject("data");
             final String picURL = obj.getString("url");
			
			/*urltest = new JSONObject(picURL1);
			final String aURL=urltest.getString("url");
			final String picURL=aURL.replace("\\","");
			Log.d("picurl", "= "+picURL);*/
			
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					//final Bitmap bitmap=Utility.getBitmap("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/23090_100001113552836_23_q.jpg");
					final Bitmap bitmap=Utility.getBitmap(picURL);
					handler.post(new Runnable() {
						public void run() {	
								text.setText(name);
								imgUserPic.setImageBitmap(bitmap);	 	
						}
					});
				}
			}).start();
			
		}
		catch(JSONException ex)
		{
			ex.printStackTrace();
		}
		
	}

	@Override
	public void onIOException(IOException e, Object state) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFileNotFoundException(FileNotFoundException e, Object state) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMalformedURLException(MalformedURLException e, Object state) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFacebookError(FacebookError e, Object state) {
		// TODO Auto-generated method stub
		
	}
	
	

}
}